package com.lavegue.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.lavegue.model.UserInfo;

/**
 * @author: luoweibin
 * @date:2011-12-19
 * description: 
 *
 */
public class StringUtil {
	
	/**
	 * 把list中的元素用指定符号连接成字符串
	 * @param strs
	 * @param splitChar
	 * @return
	 */
	public static String concat(Collection strs, String splitChar) {
		StringBuffer sb = new StringBuffer();
		int len = strs.size();
		int count = 0;
		for (Object obj : strs) {
			if (obj != null) {
				if (count == 0) {
					sb.append(obj.toString().trim());
				} else {
					sb.append(splitChar + obj.toString().trim());
				}
			}
			count ++;
		}
		
		return sb.toString();
	}
	
	public static String concat(String[] strs, String splitChar) {
		return concat(Arrays.asList(strs), splitChar);
	}
	
	/**
	 * 用逗号把String[]中的元素连接成字符串
	 * @param strs
	 * @return
	 */
	public static String concat(String[] strs) {
		return concat(Arrays.asList(strs), ",");
	}
	
	/**
	 * 用逗号把list中的元素连接成字符串
	 * @param strs
	 * @return
	 */
	public static String concat(Collection strs) {
		return concat(strs, ",");
	}
	
	public static boolean isEmpty(String str){
		if(str == null){
			return true;
		}
		if(str.trim().length()==0){
			return true;
		}
		return false;
	}
	
	public static List<String> cutEmptyEl(List<String> list) {
		List<String> newList = new ArrayList<String>();
		for (String s : list) {
			if (!isEmpty(s)) {
				newList.add(s);
			}
		}
		return newList;
	}
	
	/**
	 * 判断是否是手机号码
	 * @return
	 */
	public static boolean isAccount(String moblieNo){ 
		String regexp = "^((13[0-9]{1}|15[0-9]{1}|18[0-9]{1}|)+\\d{8})$";
		Pattern pattern = Pattern.compile(regexp);
		Matcher mp = pattern.matcher(moblieNo);
		boolean flag = mp.find();
		return flag;
	}
	/**
	 * 判断是否是电话号码
	 * @return
	 */
	public static boolean isTelPhone(String telphone){ 
		String regPatt = "^((\\d{3,4})|(\\d{3,4}-))?\\d{7,8}$";
		Pattern pattern = Pattern.compile(regPatt);
		Matcher mp = pattern.matcher(telphone);
		boolean flag = mp.find();
		return flag;
	}
	/**
	 * 判断是否是电子邮件 Email
	 * @return
	 */
	public static boolean isEmail(String email){
		String regInvalid = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
	//	String regEmail=    "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*$";
		Pattern pattern = Pattern.compile(regInvalid);
		Matcher mp = pattern.matcher(email);
		boolean flag = mp.find();
		return flag;
	}
	/**
	 * 判断是否是有效的QQ号码
	 * @return
	 */
	public static boolean isQq(String qq){
		String exgQQ = "^[1-9]\\d{4,11}$";
		Pattern pattern = Pattern.compile(exgQQ);
		Matcher mp = pattern.matcher(qq);
		boolean flag = mp.find();
		return flag;
	}
	/**
	 * 判断是否是有效的网址主页
	 * @return
	 */
	public static boolean isPersonalHonepage(String honepage){
		String expression = "((http(s)?:\\/\\/)|([\\w-]+\\.))+[\\w-]+(\\/[\\w- .\\/?%&=]*)?"; 
	//	String expURL = "^([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?$";
		Pattern pattern = Pattern.compile(expression);
		Matcher mp = pattern.matcher(honepage);
		boolean flag = mp.find();
		return flag;
	}
	/**
	 * 判断是否是有效的余额
	 * @return
	 */
	public static boolean isBalance(String balance){
		String currBal = "^\\d+(\\.\\d{0,5})?$";
		String regBal = "^\\d+(\\.\\d+)?$";
		Pattern pattern = Pattern.compile(currBal);
		Matcher mp = pattern.matcher(balance);
		boolean flag = mp.find();
		return flag;
	}
	/**
	 * 判断是否是邮政编码
	 * @return
	 */
	public static boolean isValidZipCode(String zipCode) {
		String regPattern = "^\\d{6}$";
		Pattern pattern = Pattern.compile(regPattern);
		Matcher mp = pattern.matcher(zipCode);
		boolean flag = mp.find();
		return flag;
	}
	/**
	 * 判断是不是有效的汉字
	 */
	public static boolean checkIsChinese(String strChinese) {
	    //正则表达式 /^[\u0391-\uFFE5]+$/
	    String regPattern = "[\u4e00-\u9fa5]";
	    Pattern pattern = Pattern.compile(regPattern);
		Matcher mp = pattern.matcher(strChinese);
		boolean flag = mp.find();
		return flag;
	}
	
	public static void main(String[] args) {
		///^((13[0-9]{1}|15[0-9]{1}|18[0-9]{1}|)+\d{8})$/;
	//	Pattern pattern = Pattern.compile("^((13[0-9]{1}|15[0-9]{1}|18[0-9]{1}|)+\\d{8})$");
//		List<String> aa = new ArrayList<String>();
//		aa.add("fds");
//		aa.add(" ");
//		System.out.println(StringUtil.cutEmptyEl(aa).size());
		
//		boolean bb = StringUtil.isAccount("13724253120");
//		System.out.println("res="+bb);
//		
//		boolean qq = StringUtil.isQq("12321311");
//		System.out.println("QQ-result="+qq);
//		boolean email = StringUtil.isEmail("qq@qq.com");
//		System.out.println("Email-result="+email);
//		
//		boolean honepage = StringUtil.isPersonalHonepage("https://.com");
//		System.out.println("主页-result="+honepage);
//		
//		boolean bal = StringUtil.isBalance("011");
//		System.out.println("余额-result="+bal);
//		
//		boolean chinese = StringUtil.checkIsChinese("qqa");
//		System.out.println("中文-result="+chinese);
		
		String aaString= "86 0755 12345678";
		String rep = aaString.replaceAll(" ", "");	
		String subString = rep.substring(0, 2);
		System.out.println("替换="+rep+"长度="+rep.length());
		System.out.println("长度="+subString.length()+"截取="+subString);
		if(subString.equals("86")){
			String subend = rep.substring(2,rep.length());	
			System.out.println("subend="+subend);
		}
		String replaceFirst=aaString.replaceFirst("86", "");
		System.out.println("replaceFirst="+replaceFirst);
		/*
		// 构造 XSSFWorkbook 对象，xlsFile 传入文件路径    

//		Workbook xwb = null;
//		List<Map<Integer, String>> userList = new ArrayList();
//		try {
//			xwb = new XSSFWorkbook("D:/aa.xls"); //读取2007Excel文件
//		} catch (Exception e) {
//			e.printStackTrace();
//			try {
//				xwb = new HSSFWorkbook(new FileInputStream("D:/aa.xls")); //读取2003Excel文件
//			} catch (Exception e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}  
//		}
//		try {
//			// 读取第一章表格内容    
//			Sheet sheet = xwb.getSheetAt(0);    
//			// 定义 row、cell    
//			Row row;    
//			String cell="";
//			// 循环输出表格中的内容    
//			for (int i = sheet.getFirstRowNum()+1; i < sheet.getPhysicalNumberOfRows(); i++) { //获取行数据    
//			    row = sheet.getRow(i);
//			    Map<Integer, String> map = new HashMap();
//			    boolean flag =false;
//			    int colCnt = sheet.getRow(0).getPhysicalNumberOfCells(); //获取第一行头的标题有多少列
////			    System.out.println("Row行数：="+sheet.getPhysicalNumberOfRows());
////			    System.out.println("Cell列数：="+colCnt);
//			    if (((row.getCell(0) ==null || StringUtil.isEmpty(row.getCell(0).toString()))  && (row.getCell(1) ==null || StringUtil.isEmpty(row.getCell(1).toString())))) {
//			    	continue;
//			    }
//			    for (int j = row.getFirstCellNum(); j < colCnt; j++) { //获取列数据
//			        // 通过 row.getCell(j).toString() 获取单元格内容，
//			    	if (row.getCell(j) == null) {
//			    		map.put(j, "");
//			    	}else{
//			    		//row.getCell(j).getStringCellValue();
//			    		if ((j == 1 || j == 2 || j == 4 || j == 11) && (row.getCell(j).getCellType() ==0)) {
//			    			DecimalFormat dFormat = new DecimalFormat("####");
//			    			cell = dFormat.format(Double.parseDouble(row.getCell(j).toString()));
//			    				//row.getCell(j).toString();
//			    			
//			    			System.out.println(cell + "j="+j+" 列的类型："+row.getCell(j).getCellType());
//			    		} else {
//			    			cell = row.getCell(j).toString();
//			    			System.out.println("====="+cell + "  类型啊：" + row.getCell(j).getCellType());
//			    		}
//					     map.put(j, cell);
//			    	}
//			       
//			       // System.out.print(cell + "/t");    
//			    }
//			    if (!(StringUtil.isEmpty(map.get(0))  && StringUtil.isEmpty(map.get(1)))) {
//			    	userList.add(map);//获取一行所有的列的数据放到list里
//			    }
//			}
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}    
//		
//		
//		for (Map<Integer, String> m : userList) {
//			System.out.println(m.get(1));
//		}
		BigDecimal g = new BigDecimal("1");
		BigDecimal d = g.divide(new BigDecimal("100"));
		System.out.println(d.toString());

		Workbook xwb = null;
		List<Map<Integer, String>> userList = new ArrayList();
		try {
			xwb = new XSSFWorkbook("D:/aa.xls"); //读取2007Excel文件
		} catch (Exception e) {
			e.printStackTrace();
			try {
				xwb = new HSSFWorkbook(new FileInputStream("D:/aa.xls")); //读取2003Excel文件
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}  
		}
		try {
			// 读取第一章表格内容    
			Sheet sheet = xwb.getSheetAt(0);    
			// 定义 row、cell    
			Row row;    
			String cell="";
			// 循环输出表格中的内容    
			for (int i = sheet.getFirstRowNum()+1; i < sheet.getPhysicalNumberOfRows(); i++) { //获取行数据    
			    row = sheet.getRow(i);
			    Map<Integer, String> map = new HashMap();
			    boolean flag =false;
			    int colCnt = sheet.getRow(0).getPhysicalNumberOfCells(); //获取第一行头的标题有多少列
//			    System.out.println("Row行数：="+sheet.getPhysicalNumberOfRows());
//			    System.out.println("Cell列数：="+colCnt);
			    if (((row.getCell(0) ==null || StringUtil.isEmpty(row.getCell(0).toString()))  && (row.getCell(1) ==null || StringUtil.isEmpty(row.getCell(1).toString())))) {
			    	continue;
			    }
			    for (int j = row.getFirstCellNum(); j < colCnt; j++) { //获取列数据
			        // 通过 row.getCell(j).toString() 获取单元格内容，
			    	if (row.getCell(j) == null) {
			    		map.put(j, "");
			    	}else{
			    		//row.getCell(j).getStringCellValue();
			    		if ((j == 1 || j == 2 || j == 4 || j == 11) && (row.getCell(j).getCellType() ==0)) {
			    			DecimalFormat dFormat = new DecimalFormat("####");
			    			cell = dFormat.format(Double.parseDouble(row.getCell(j).toString()));
			    				//row.getCell(j).toString();
			    			
			    			System.out.println(cell + "j="+j+" 列的类型："+row.getCell(j).getCellType());
			    		} else {
			    			cell = row.getCell(j).toString();
			    			System.out.println("====="+cell + "  类型啊：" + row.getCell(j).getCellType());
			    		}
					     map.put(j, cell);
			    	}
			       
			       // System.out.print(cell + "/t");    
			    }
			    if (!(StringUtil.isEmpty(map.get(0))  && StringUtil.isEmpty(map.get(1)))) {
			    	userList.add(map);//获取一行所有的列的数据放到list里
			    }
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		
		
		for (Map<Integer, String> m : userList) {
			System.out.println(m.get(1));
		}
		*/
		System.out.println(new BigDecimal(200).compareTo(new BigDecimal(399)));
	}
	
	
	
}
